//入口文件main.js

import Vue from 'vue'
import App from './App.vue'
import moment from 'moment'
import router from './router'
import './plugins/element.js'

// import './plugins/jquery-3.2.1.min.js'
// import './plugins/base64-1.7.js'
import _ from 'lodash'
let Base64 = require('js-base64').Base64
// import moment from 'moment'
import $ from 'jquery'
//导入全局样式表
import './assets/fonts/iconfont.css'
//导入全局样式表
import './assets/css/global.css'
import './assets/css/common.less'
import breadcrumb from './components/common/breadcrumb'

//
import global from './components/common/global/global'
import qs from 'qs'

import httpRequest from './utils/httpRequest' // api: https://github.com/axios/axios

//配置axios请求
import axios from 'axios'
//配置请求的路径
// axios.defaults.baseURL='http://www.klxin.cn:8888/api/private/v1/'
// axios.defaults.baseURL='http://192.168.0.99:8080/sbsaas/api/'


// http://192.168.0.99:8080/sbsaas/api/login

// let token = window.sessionStorage.getItem('token');
//   // console.log(token)
//   axios.defaults.headers.common['token'] = token;

Vue.component("breadcrumb", breadcrumb)

// axios.interceptors.request.use(config => {
//   // console.log(config)
//   config.headers.Authorization = window.sessionStorage.getItem('token')
//   // 在最后必须 return config
//   return config
//   })

  let token = window.sessionStorage.getItem('token');
  // console.log(token)
  axios.defaults.headers.common['token'] = token;

//把axios 挂载在原型下 this.$http=== axios
// Vue.prototype.$http=axios
httpRequest.defaults.baseURL = '/api'
Vue.prototype.$http = httpRequest

// 配置全局属性
Vue.prototype.$global = global
Vue.prototype.$moment = moment

Vue.prototype.$qs = qs


Vue.config.productionTip = false

//注册全局时间过滤
Vue.filter('dateformat', function(dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
    return moment(dataStr).format(pattern)

})

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')



//得到文件流后下载文件
// 拿到数据以后 通过 new Blob对象 创建excel
Vue.prototype.$downloadFile = function downloadFile(url, method, params, fileType, fileName) {
  var httpJson = {
    url: url,
    method: method,
    responseType: 'blob'
  }
  if (method === 'get' || method === 'GET') {
    httpJson.params = params
  } else if (method === 'post' || method === 'POST') {
    httpJson.data = params
  }
  httpRequest(httpJson).then((response) => {
    var data = response.data
    var headers = response.headers
    if (!data) {
      return
    }
    var fName = decodeURI(headers.filename)
    var fType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'
    var fileFix = ".xlsx"
    if(fileType){
      fType = fileType.blobType
      fileFix = fileType.fileFix
    }
    if(fName==undefined || fName==null && fName===''){
      if(fileName){
        fName = fileName+fileFix
      }else{
        fName = "导出文件.xlsx"
      }
    }
    const blob = new Blob([data], {type: fType})
    const downloadElement = document.createElement('a')
    const href = window.URL.createObjectURL(blob)
    downloadElement.href = href
    downloadElement.download = fName
    document.body.appendChild(downloadElement)
    downloadElement.click()
    document.body.removeChild(downloadElement) // 下载完成移除元素
    window.URL.revokeObjectURL(href) // 释放掉blob对象
  })
}
// new Vue({
//   el: '#app',
//   router: router,
//   render: h => h(app)
//   render:function (h) {return function h(app)}
// }) 一致
